using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
using System.Collections.ObjectModel;

namespace Inline.SharePoint.SecurityOnViews.HttpModule.Features.Inline.SharePoint.SecurityOnViews.HttpModule
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("08c3a498-c360-4997-953d-06c2aa8b1f04")]
    public class InlineSharePointSecurityOnViewsEventReceiver : SPFeatureReceiver
    {

        private const string webConfigModName = "Inline.SharePoint.SecurityOnViews.HttpModule.SecurityOnViews";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            SPSite site = properties.Feature.Parent as SPSite;

            if (site.RootWeb.Lists.TryGetList("Inline.SharePoint.SecurityOnViews") == null)
            {

                site.RootWeb.Lists.Add("Inline.SharePoint.SecurityOnViews", "Holds the security on views configuration", SPListTemplateType.GenericList);

            }

            SPWebApplication webApp = site.WebApplication;

            SPWebConfigModification webConfigModification = GetWebConfigModificationByName(webConfigModName, webApp.WebConfigModifications);
            if (webConfigModification == null)
            {

                SPWebConfigModification myModification = new SPWebConfigModification();
                myModification.Path = "configuration/system.webServer/modules";
                myModification.Name = "add[@name='Inline.SharePoint.SecurityOnViews.HttpModule'][@type='Inline.SharePoint.SecurityOnViews.HttpModule.SecurityOnViews, Inline.SharePoint.SecurityOnViews.HttpModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c8b0f5ff5db69fd5']";
                myModification.Sequence = 0;
                myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                myModification.Value = @"<add name=""Inline.SharePoint.SecurityOnViews.HttpModule"" type=""Inline.SharePoint.SecurityOnViews.HttpModule.SecurityOnViews, Inline.SharePoint.SecurityOnViews.HttpModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c8b0f5ff5db69fd5"" />";

                webApp.WebConfigModifications.Add(myModification);
                webApp.Update();
                webApp.WebService.ApplyWebConfigModifications();

            }

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {

            SPSite site = (SPSite)properties.Feature.Parent;

            SPList list = site.RootWeb.Lists.TryGetList("Inline.SharePoint.SecurityOnViews");
            if (list != null)
            {

                site.RootWeb.Lists.Delete(list.ID);

            }

            SPWebApplication webApp = site.WebApplication;

            SPWebConfigModification webConfigModification = GetWebConfigModificationByName(webConfigModName, webApp.WebConfigModifications);
            if (webConfigModification != null)
            {

                webApp.WebConfigModifications.Remove(webConfigModification);
                webApp.WebService.ApplyWebConfigModifications();
                webApp.Update();

            }

        }

        private SPWebConfigModification GetWebConfigModificationByName(string name, Collection<SPWebConfigModification> webConfigModifications)
        {

            for (int i = 0; i < webConfigModifications.Count; i++)
            {

                if (webConfigModifications[i].Name == webConfigModName)
                    return webConfigModifications[i];

            }

            return null;

        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
